package com.jm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jm.common.util.entity.R;
import com.jm.common.util.entity.SelectCondition;
import com.jm.pojo.Setmeal;
import com.jm.pojo.SetmealToy;
import com.jm.pojo.dto.SetmealDto;
import com.jm.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    @Cacheable(value = "SetmealCache",key = "#result.data")
    public R<IPage<SetmealDto>> selectByPageCondition(SelectCondition condition) {
        IPage<Setmeal> setmealIPage = setmealService.selectByPageCondition(condition);
        IPage<SetmealDto> setmealDtoIPage = setmealService.toDto(setmealIPage);
        return R.success(setmealDtoIPage);
    }

    /**
     * 套餐添加
     */
    @PostMapping
    @CacheEvict(value = "SetmealCache",allEntries = true)
    public R<String> addOne(@RequestBody SetmealDto setmealDto) {
        boolean save = setmealService.saveWithSetmealToy(setmealDto);
        if (save) return R.success("保存套餐成功");
        return R.error("保存套餐失败");
    }

    /**
     * 删除/批量删除
     */
    @DeleteMapping()
    @CacheEvict(value = "SetmealCache",allEntries = true)
    public R<String> delete(@RequestParam("ids") List<Long> ids) {
        boolean result = setmealService.removeBatchByIds(ids);
        if (result) return R.success("删除操作成功");
        return R.error("删除操作失败");
    }

    /**
     * 状态修改/批量状态修改
     */
    @PostMapping("/status/{status}")
    public R<String> changeSetmealStatus(@PathVariable Integer status, @RequestParam("ids") List<String> ids) {
        boolean b = setmealService.changeStatusByIds(ids, status);
        if (b) return R.success("状态修改操作成功");
        return R.error("状态修改操作失败");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> getByCategoryId(String categoryId) {
        List<Setmeal> setmealList = setmealService.getByCategoryId(categoryId);
        if (setmealList == null || setmealList.size() == 0) return R.error("未查询到合适的玩具列表");
        return R.success(setmealList);
    }

    /**
     * 获得指定套餐的toy列表
     */
    @GetMapping("/toy/{setmealId}")
    @Cacheable(value="SetmealCache",key ="#setmeal.id")
    public R<List<SetmealToy>> getToyBySetmealId(@PathVariable Long setmealId) {
        SetmealDto setmealDto = setmealService.getDtoBySetmealId(setmealId);
        List<SetmealToy> setmealToys = setmealDto.getSetmealToys();
        return R.success(setmealToys);
    }

    /**
     * 根据ID获得SetmealDto
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.selectWithToy(id);
        if (setmealDto == null) return R.error("获得数据失败");
        return R.success(setmealDto);
    }

    @PutMapping
    @Cacheable(value = "SetmealCache",key = "#result.data")
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        boolean b = setmealService.updateWithDish(setmealDto);
        if (b) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}
